package lan.agents;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.wrapper.gateway.GatewayAgent;
import lan.bean.BlackBoardBean;

public class MyGateWayAgent extends GatewayAgent {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BlackBoardBean board = new BlackBoardBean();
	protected void setup(){
		super.setup();
		
	}

	protected void processCommand(java.lang.Object object){
		if(object instanceof BlackBoardBean){
			board = (BlackBoardBean)object;
			ACLMessage  msg = new ACLMessage(ACLMessage.REQUEST);
		//	AID receiver = new AID;
		//	receiver.addAddresses("http://toshiba-PC:7778/acc");
			//msg.addReceiver(new AID(board.getReceiver(), AID.ISLOCALNAME));
			msg.setContent(board.getMessage());
			send(msg);
			addBehaviour(new ReceiveMessage(this));
			
		}
		
	}

class ReceiveMessage extends CyclicBehaviour{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public ReceiveMessage(Agent a){
		super(a);
	}
	public void action(){
		ACLMessage msgreceive = receive();
		if(msgreceive == null){
			block();
			return;
		}
		
		if(board!= null){
			board.setMessage(msgreceive.getContent());
			System.out.println("[MyGateWayAgent]"+board.getReceiver());
			System.out.println("[MyGateWayAgent]"+board.getMessage());
			releaseCommand(board);
		}
	}
}
}
